package com.cg.asm.body.util;


import com.cg.asm.client.AsmSession;
import com.cg.asm.client.domain.menu.MenuUrlLoginVO;
import com.cg.asm.client.domain.user.UserInfo;
import com.cg.asm.client.exception.MybatisExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AsmBodyCommonUtil {

    /**
     * 获取当前登陆用户信息
     * @return
     */
    public static UserInfo getCurrentUserLoginVO() {
        UserInfo userLoginVO;
        try {
            userLoginVO = AsmSession.getSessionUserInfo(AsmBodyCommonUtil.getRequest());
        } catch (NullPointerException e) {
            userLoginVO = null;
        }
        return userLoginVO;
    }

    /**
     * 分页对象中的ParameterMap合并
     *
     * @param mybatisExample
     * @param param
     */
    public static Map<String, String> mybatisExampleMergeParam(MybatisExample mybatisExample, Map<String, String> param) {
        Map<String, String> parameterMap = (Map<String, String>) mybatisExample.getParameter();
        if (null == parameterMap) {
            parameterMap = new HashMap<>();
        }
        if (null != param && param.size() != 0) {
            parameterMap.putAll(param);
        }
        return parameterMap;
    }

    /**
     * 功能点授权查询
     *
     * @param userPoints
     * @param permissionValue
     * @return
     */
    public static boolean permission(List<MenuUrlLoginVO> userPoints, String permissionValue) {
        boolean permissionResult = false;
        if (StringUtils.isNotEmpty(permissionValue)) {
            for (MenuUrlLoginVO mo : userPoints) {
                if (permissionValue.equals(mo.getFunPointsCode())) {
                    permissionResult = true;
                    break;
                }
            }
        }
        return permissionResult;
    }

    /**
     * 根据上下文获取Request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
}
